<?php
namespace App\Admin\Controllers\Consultation;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use App\Admin\Controllers\Common\Common;

class PondController extends Controller {
	public function index(Request $r) {
		return view("admin.consultation.pond.index");
	}

	public function getList(Request $r) {
		$rows = self::mkQuery($r);
		$total = 3;
		return ['code'=>1,'msg'=>"加载成功",'data'=>[
			"rows" => $rows,
			"total" => $total
		]];
	}

	private static function mkQuery($param) {
		// 年龄段数组
		$age_cfg = DB::table("consultation_age_cfg")->getArr();
		$age_arr = [];
		foreach($age_cfg as $v){
			$age_arr[$v['class']] = [
				'min' => $v['age_min'],
				'max' => $v['age_max']
			];
		}
		// 该班级已经有的
		$new_class_ids = DB::table('consultation_newclass')->where("start",'=',1)->pluck("id")->toArray();
		$students = DB::table('consultation_st')->whereIn('class_id',$new_class_ids)->getArr(['sex','birthday','level']);
		$count_male = 0;
		$count_female = 0;
		$count_A = 0;
		$count_B = 0;
		$count_C = 0;
		$count_D = 0;
		foreach($students as $v){
			if($v['sex'] == "男"){
				$count_male --;
			}else{
				$count_female --;
			}
			switch ($v['level']) {
				case 'A':
					$count_A --;break;
				case 'B':
					$count_B --;break;
				case 'C':
					$count_C --;break;
				case 'D':
					$count_D --;break;
			}
		}
		// 该班级欠缺的
		$new_class = DB::table('consultation_newclass')->where("start",'=',1)->getArr();
		foreach($new_class as $v){
			$count_male += $v['male'];
			$count_female += $v['female'];
			$count_A += $v['lvl_a'];
			$count_B += $v['lvl_b'];
			$count_C += $v['lvl_c'];
			$count_D += $v['lvl_d'];
		}

		$rows = DB::table('consultation_list as list')
			->leftJoin('consultation_st as st','list.st_id','=','st.id')
			->where([
				"list.type" => 7,
				"list.status" => 6,
				"list.pond" => (int)$param['pond']
			])->getArr(['st.*']);
		return $rows;
	}
}
